import { EBagType, EBundleType, EDropType } from "../Common/Enum";
import { LoadUtil } from "../Util/LoadUtil";

export enum EConfigType {
    All = 'all',
}

export interface ISheetConfig {
    item: IItemConfig[];
}

export interface IItemConfig {
    id: number;
    name: string;
    drop: EDropType;
    type: EBagType;
    num: string;
    basePrice: number;
}



export default class BaseConfig {
    private static _instance: BaseConfig = null;
    public static get instance() {
        if (this._instance === null) {
            this._instance = new this();
        }
        return this._instance;
    }

    private jsonDatas: Map<string, ISheetConfig> = new Map();

    public isLoadComplete: boolean = false;

    public initJsonInfos() {
        // json
        this.setJsonInfos(EBundleType.Game, "json/all");
    }

    private setJsonInfos(bundle: EBundleType, url: string) {
        LoadUtil.instance.loadRes(bundle, url, cc.JsonAsset, (err, res) => {
            this.isLoadComplete = true;
            this.jsonDatas.set(res.name, res.json);
        });
    }

    /**
     * 多配置整合的json
     * @param name 
     * @param sheet 
     * @returns 
     */
    public get<T extends keyof ISheetConfig>(name: string, sheet: T) {
        const configs = this.jsonDatas.get(name);
        if (configs && sheet) {
            return configs[`${sheet}`];
        }
        return null;
    }

    public getItemConfig() {
        const config = this.get('all', 'item');
        return config;
    }

    /**
     * 根据掉落区域、类型获取
     * @param type 
     * @returns 
     */
    public getItemInfosByType(drop: EDropType, type: EBagType) {
        const config = this.getItemConfig();
        const arr: IItemConfig[] = Object.keys(config).map(key => config[key]);
        const infos = arr.filter((o) => o.drop == drop && o.type == type);
        return infos;
    }

    /**
     * 根据id获取物品表数据
     * @param id 
     * @returns 
     */
    public getItemInfoById(id: number) {
        const config = this.getItemConfig();
        const arr: IItemConfig[] = Object.keys(config).map(key => config[key]);
        const info = arr.find((o) => o.id == id);
        return info;
    }
}